var express = require('express');
var router = express.Router();
var request = require('request');
var qs = require('qs');
var path = require('path');

router.get('/',function(req,res) {//转账主页
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/account.html"));
    }else {
        res.redirect("/");
    }
});

router.post('/checkToAccount',function(req,res) {//转账账户
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/transfer/checkToAccount";
        var data ={
            "tocardNo" : req.body.tocardNo
        }
        request({
            url: url,
            method: "post",
            headers: { 
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100,
            },
            body: qs.stringify(data)
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
                if(JSON.parse(_resBody).code == 0) {
                    req.session.username = JSON.parse(_resBody).data.username;
                    req.session.tocardNo = JSON.parse(_resBody).data.tocardNo;
                }
                console.log(JSON.parse(_resBody).code);
                res.send(_resBody);
             }else {
                 console.log(_err);
             }
        });
    }else {
        res.redirect("/");
    }
});

router.get('/amount',function(req,res) { //转账金额页面
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/transfer.html"));
    }else {
        res.redirect("/");
    }
});

router.post('/money',function(req,res) {//保存转账金额
    if(req.session.token) {
        req.session.amount = req.body.amount;
        res.end();
    }else {
        res.redirect("/");
    }
});

router.get('/confirm',function(req,res) { //转账信息确认
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/confirm.html"))
    }else {
        res.redirect("/");
    }
});

router.post('/confirm2',function(req,res) {//转账信息确认初始化
    if(req.session.token) {
        var data = {
            "tocardNo" : req.session.tocardNo,
            "username" : req.session.username,
            "amount" : req.session.amount,
            "fee" : 0
        }
        res.send(JSON.stringify(data));
    }else {
        res.redirect("/");
    }
});

router.get('/password',function(req,res) { //密码页面
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/password.html"));
    }else {
        res.redirect("/");
    }
});
router.post('/checkpwd',function(req,res) {//验证密码
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/transfer/checkpwd";
        var data ={
            "tocardNo" : req.session.tocardNo,
            "amount" : req.session.amount,
            "password" : req.body.password
        }
        request({
            url: url,
            method: "post",
            headers: { 
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100,
            },
            body: qs.stringify(data)
        },function(_err,_res,_resBody) {
            var code = JSON.parse(_resBody).code;
            if(!_err && _res.statusCode == 200) {
                if(code == 0) {  //???
                    req.session.adviceId = JSON.parse(_resBody).data.adviceId;//凭条id
                }
                res.send(_resBody);
             }else {
                 console.log(_err);
             }
        });
    }else {
        res.redirect("/");
    }
});


router.get('/successfultransfer',function(req,res) {//转账成功页面
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/successfultransfer.html"));
    }else {
        res.redirect("/");
    }
});
router.post('/successfultransfer_2',function(req,res) {//转账成功页面ajax
    if(req.session.token) {
        var data = {
            "amount" : req.session.amount,
            "fee" : 0
        }
        res.send(JSON.stringify(data));
    }else {
        res.redirect("/");
    }
});
module.exports = router;